// MyWin32Project.cpp : Defines the entry point for the application.
//tortoise 24/05
//#include "MyWin32Project.h"
#include "Stdafx.h"
#include "Resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HWND hOutput;
HWND hInput;
HWND hUserName;
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING] ;					//?? The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);





void appendOutput(TCHAR* newText)
{
	
}



int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_MYWIN32PROJECT, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MYWIN32PROJECT));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}











//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYWIN32PROJECT));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_APPWORKSPACE+3);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_MYWIN32PROJECT);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));


	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
   // CreateWindow function has 11 parameters
   hWnd = CreateWindow(szWindowClass,			//1) lpClassName [in, optional] Type: LPCTSTR
					   szTitle,					//2) lpWindowName [in, optional] Type: LPCTSTR
					   WS_OVERLAPPEDWINDOW,		//3) dwStyle [in] Type: DWORD
					   CW_USEDEFAULT,			//4) x [in] Type: int
					   0,						//5) y [in] Type: int
	 /*CW_USEDEFAULT*/ 700,						//6) nWidth [in] Type: int
					   500,						//7) nHeight [in] Type: int
					   NULL,					//8) hWndParent [in, optional] Type: HWND
					   NULL,					//9) hMenu [in, optional] Type: HMENU
					   hInstance,				//10) hInstance [in, optional] Type: HINSTANCE
					   NULL						//11) lpParam [in, optional] Type: LPVOID
					   );

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	//TCHAR testStr[] =_T("Test String");
	TCHAR text[50] = _T("Your Name: ");
	TCHAR sendMsg[30] = TEXT("You Clicked vSend");
	

	switch (message)
	{
		
		case WM_CREATE:
		{
        HFONT hfDefault;
	
       
		HWND hSend;
		HWND hwndProgress;

		//=========================== Get User name ===============================
    hUserName = CreateWindowEx(WS_EX_CLIENTEDGE,
							"EDIT",
							NULL,
							WS_CHILD | WS_VISIBLE , 
							100,
							10,
							350,
							25,
							hWnd,
							(HMENU)IDC_GET_USER,
							GetModuleHandle(NULL),
							NULL);

    if(hUserName == NULL)
	{
        MessageBox(hWnd, "Could not create hUserName box.","Error", MB_OK | MB_ICONERROR);
	}
		
	//=========================== Message Display ===============================
        hOutput = CreateWindowEx(WS_EX_CLIENTEDGE,
							   "EDIT",
							   NULL,
							   WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE, 
							   100,
							   50,
							   350,
							  200,
							   hWnd,
							   (HMENU)IDC_MAIN_EDIT,
							   GetModuleHandle(NULL),
							   NULL);

        if(hOutput == NULL)
		{
            MessageBox(hWnd, "Could not create Output box.","Error", MB_OK | MB_ICONERROR);
		}

		//======================== Text Input  box ==============================
		        hInput = CreateWindowEx(WS_EX_CLIENTEDGE,
							   "EDIT",
							   NULL,
							   WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE, 
							   100,
							   270,
							   350,
							   100,
							   hWnd,
							   (HMENU)IDC_MAIN_EDIT,
							   GetModuleHandle(NULL),
							   NULL);

        if(hInput == NULL)
		{
            MessageBox(hWnd, "Could not create Input box.","Error", MB_OK | MB_ICONERROR);
		}

		//================================= Send Button ===================================
        hSend = CreateWindowEx(WS_EX_CLIENTEDGE,
							   "BUTTON", // class type
							   "Send", // text 
							   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
							   100,	// x co-ord
							   380,	// y co-ord
							   150, // length
							   30,  // height
							   hWnd,
							   (HMENU)IDC_MAIN_SEND,
							   GetModuleHandle(NULL),
							   NULL);
        if(hSend == NULL)
		{
            MessageBox(hWnd, "Could not create Button.","Error", MB_OK | MB_ICONERROR);
		}

		 

    }
    break;

	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_CONN:

			ShowWindow(hUserName,SW_SHOWNOACTIVATE);
			TCHAR fieldVal[50];
			if(SendMessage(hUserName ,WM_GETTEXT,sizeof(fieldVal)/sizeof(fieldVal[0]),(LPARAM)fieldVal))
			{

				//MessageBox(hWnd,"Connect Ok","Title",MB_OK);
				EnableWindow(hUserName,false);
				SetFocus(hInput);
			}
			else
			{
				MessageBox(hWnd,"You must enter A Name","Your Name",MB_OK);
				SetFocus(hUserName);
			}

			//MessageBox(hWnd,"You Clicked\nConnect","Title",MB_OK);
			
			//DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd,);
			break;
		case IDM_DCONN:
			EnableWindow(hUserName,true);
			SetWindowText(hUserName, "" );
			SetFocus(hUserName);
			//MessageBox(hWnd,"You Clicked\nDisconnect","Title",MB_OK);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;

		case IDC_MAIN_SEND:
			
			// retrieve Text from input box 
			TCHAR buffer[300];

			if(SendMessage(hInput ,WM_GETTEXT,sizeof(buffer)/sizeof(buffer[0]),(LPARAM)buffer))
			{
				// ???Write\Append  text to Display box and send to server or
				// display server response this means the server is responsible for appending text messages 
				SendMessage(hOutput,WM_SETTEXT,sizeof(buffer)/sizeof(buffer[0]),(LPARAM)buffer);

				// Clear input field
				SetWindowText( hInput, "" );
				SetFocus(hInput);
			}


			

			
			//SendMessage(hInput,WM_CLEAR,sizeof(buffer)/sizeof(buffer[0]),(LPARAM)buffer);
			 

			

			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		//SetTextColor(hdc, RGB(0, 0, 0));

		
		TextOut(hdc,15,12,text, 10);

		//MessageBox(hWnd,"","Title",MB_OK);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
